#!/bin/bash

echo "mkbootable"
if [ $EUID -ne 0 ]; then
echo "need admin privileges"
osascript << EOQ
do shell script "'$0' $1" with administrator privileges
EOQ
else
echo "$syslinuxcmd $1"
syslinuxcmd=`echo "$0" | sed s/mkbootable/syslinux-mac/g`
"$syslinuxcmd" "$1"
sync
diskutil umount "$1"
sync
hdiutil unmount "$1"
sync
rawdev=`echo "$1" | sed "s/s[^s]*$//"`
partnum=`echo "$1" | perl -l -ne '/s[0-9]+(?!.*s)/ && print $&' | perl -l -ne '/[0-9]+/ && print $&'`
echo "partnum: $partnum rawdev: $rawdev"
fdisk -y -e "$rawdev" << EOR
flag $partnum
write
y
quit
EOR
echo "writing mbr from $mbrfile to $rawdev"
mbrfile=`echo "$0" | sed s/mkbootable/mbr.bin/g`
sync
diskutil umount "$1"
sync
hdiutil unmount "$1"
sync
dd if="$mbrfile" of="$rawdev"
echo "done writing mbr"
fi
